//
//  PlayerManager.m
//  Player
//
//  Created by Li on 15/6/10.
//  Copyright (c) 2015年 Li. All rights reserved.
//

#import "PlayerManager.h"

@implementation PlayerManager

- (void)dealloc{
    NSLog(@"播放器被释放了");
    [_avplayer release];
    [_avplayerItem release];
    [_musicArray release];
    [super dealloc];
}

//单例方法
+ (instancetype)sharedManager{
    static PlayerManager *manager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[PlayerManager alloc] init];
    });
    return manager;
}

- (instancetype)init{
    self = [super init];
    if (self) {
        //初始化播放器、播放内容、音乐数组
        _avplayer = [[AVPlayer alloc] init];
        _avplayerItem = [[AVPlayerItem alloc] init];
        _musicArray = [[NSMutableArray alloc] init];
        _playIndex = 0;
        
    }
    return self;
}

- (void)setMusicArray:(NSMutableArray *)musicArray{
    if (_musicArray != musicArray) {
        [_musicArray release];
        _musicArray = [musicArray retain];
    }
    //设置数组的时候初始化一个播放内容
    self.avplayerItem = [[AVPlayerItem alloc] initWithURL:[NSURL URLWithString:_musicArray[0]]];
    //给播放器设置准备播放的内容
    [self.avplayer replaceCurrentItemWithPlayerItem:_avplayerItem];
    
}

//播放
- (void)play{
    [_avplayer play];
}

//暂停
- (void)pause{
    [_avplayer pause];
}

//下一首
- (void)nextMusic{
    //判断当前的播放模式
    if (_playModel == PlayModelRandom) {
        _playIndex = arc4random() % _musicArray.count;
    } else {
        _playIndex++;
    }
    if (_playIndex >= 9) {
        _playIndex = 0;
    }
    [self changeMusicWithIndex:_playIndex];
}



//上一首
- (void)beforeMusic{
    //判断当前的播放模式
    if (_playModel == PlayModelRandom) {
        _playIndex = arc4random() % _musicArray.count;
    } else {
        _playIndex--;
    }
    if (_playIndex < 0) {
        _playIndex = _musicArray.count - 1;
    }
    [self changeMusicWithIndex:_playIndex];
}

//返回当前的播放时间
-(NSInteger)currentTime
{
    CMTime time = _avplayer.currentTime;
    if (time.timescale == 0) {
        return 0;
    }
    return (NSInteger)(time.value / time.timescale);
}

//返回总时间
-(NSInteger)duration
{
    CMTime time = _avplayerItem.duration;
    if (time.timescale == 0) {
        return 0;
    }
    return (NSInteger)(time.value / time.timescale);
}

//重写getter方法
- (PlayState)playState
{
    if (_avplayer.rate == 0.0) {
        return PlayStatePause;
    }
    return PlayStatePlay;
}

//根据下标来切换音乐
- (void)changeMusicWithIndex:(NSInteger)index{
    _playIndex = index;
    self.avplayerItem = [[AVPlayerItem alloc] initWithURL:[NSURL URLWithString:_musicArray[index]]];
    [self.avplayer replaceCurrentItemWithPlayerItem:_avplayerItem];
}

//播放完毕后的处理
- (void)musicDidFinishPlay
{
    switch (_playModel) {
        case PlayModelSingleCycle:{
            [self play];
            break;
        }
        case PlayModelRandom:
        case PlayModelListCycle:{
            [self nextMusic];
            break;
        }
        case PlayModelOrder:{
            if (_playIndex < _musicArray.count - 1) {
                [self nextMusic];
            }
            break;
        }
        default:
            break;
    }
}

//跳转播放时间的方法
- (void)seekNewTime:(NSInteger)newTime
{
    CMTime time = _avplayerItem.currentTime;
    time.value = time.timescale * newTime;
    [_avplayer seekToTime:time];
}









@end
